package com.leetcode.tree;

import com.study.common.TreeNode;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @program: leetcode
 * @author: jzhou
 * @date: 2022-11-19 10:44
 * @version: 1.0
 * @description: 二叉树的最大深度
 **/
public class TreeMaxDepth {
    public int maxDepth(TreeNode root) {

        if(root == null) return 0;
        Queue<TreeNode> queue = new LinkedList<>();
        int deep = 0;
        queue.offer(root);
        while (!queue.isEmpty()){
            int size = queue.size();
            while (size > 0){
                root = queue.poll();
                if (root.right != null) queue.offer(root.right);
                if (root.left != null) queue.offer(root.left);
                size--;
            }
            deep++;
        }

        return deep;

    }
}
